package com.gray.blog.interceptor;

import com.gray.blog.common.enums.ErrorCodeEnum;
import com.gray.blog.common.util.ResultUtil;
import com.gray.blog.interceptor.pointcut.ServicePointCut;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * Created  By zhoux 2019/04/16
 **/
@Slf4j
@Aspect
@Order(3)
@Component
public class ServiceInterceptor extends ServicePointCut{

    @Around("servicePointCut()")
    public Object sysInterceptor(ProceedingJoinPoint joinPoint) throws Throwable{
        Object result = null;
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        try {
            result = joinPoint.proceed();
        } catch (Exception e) {
            log.error("ServiceInterceptor.sysInterceptor error e:{}", e);
            ResultUtil.buildErrorResult(ErrorCodeEnum.SYSTEM_ERROR, response);
            return null;
        }
        return result;
    }

}
